/*
 * ControlPanel.cpp
 *
 *  Created on: 27-12-2010
 *      Author: Przemek
 */

#include "customUI/ControlPanel.hpp"
#include "gui/Button.hpp"
#include "gui/Label.hpp"
#include "gui/Checkbox.hpp"
#include "listener/StartStopButtonListener.hpp"
#include "listener/SpeedLabelAndSliderSynchronizator.hpp"
#include "listener/AddIndividualButtonMouseListener.hpp"
#include "listener/AddBarrierButtonMouseListener.hpp"
#include "listener/AddMeatButtonMouseListener.hpp"
#include "listener/AddVegetableButtonMouseListener.hpp"
#include "listener/AddWaterButtonMouseListener.hpp"
#include "listener/DeleteSelectedElementMouseListener.hpp"
#include "listener/NextWorldStepButtonListener.hpp"
using namespace boost;
using namespace std;

ControlPanel::ControlPanel(const Dimension& parentSize,
		SelectElementMouseListener* selectElementMouseListener) :
	creationState(new CreationState)
{
	const float labelHeight = 20.0f;
	const Dimension fullLineLabelSize(parentSize.getWidth(), labelHeight);
	const Dimension halfLineLabelSize(static_cast<int> (fullLineLabelSize.getWidth()) / 2,
			static_cast<int> (fullLineLabelSize.getHeight()));
	setSize(parentSize);

	float y = 0.0f;
	Label* label = new Label("___Time___", GLColor::RED);
	label->setPosition(Vector(0.0f, y));
	label->setSize(fullLineLabelSize);
	add(label);

	y += labelHeight;
	label = new Label("Speed:   ");
	label->setPosition(Vector(0.0f, y));
	label->setSize(halfLineLabelSize);
	add(label);

	speedSlider = new Slider(Range<int> (1, 10));
	speedSlider->setValue(1);
	speedSlider->setPosition(Vector(halfLineLabelSize.getWidth(), y));
	speedSlider->setSize(halfLineLabelSize);
	add(speedSlider);

	y += labelHeight;
	label = new Label();
	label->setPosition(Vector(halfLineLabelSize.getWidth(), y));
	label->setSize(halfLineLabelSize);
	SpeedLabelAndSliderSynchronizator* synchronizator = new SpeedLabelAndSliderSynchronizator(label,
			speedSlider);
	synchronizator->mouseReleased(MouseListener::EventArguments());
	speedSlider->addManagedMouseListener(synchronizator);

	add(label);

	y += labelHeight;
	Button* startStopButton = new Button();
	startStopButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	startStopButton->setSize(halfLineLabelSize);
	startStopButton->addManagedMouseListener(new StartStopButtonListener(startStopButton));
	add(startStopButton);

	y += labelHeight;
	Button* nextStepButton = new Button("Next step");
	nextStepButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	nextStepButton->setSize(halfLineLabelSize);
	nextStepButton->addManagedMouseListener(new NextWorldStepButtonListener());
	add(nextStepButton);

	y += labelHeight;
	Button* deleteSelectedButton = new Button("Delete selected");
	deleteSelectedButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	deleteSelectedButton->setSize(halfLineLabelSize);
	deleteSelectedButton->addManagedMouseListener(new DeleteSelectedElementMouseListener(
			selectElementMouseListener));
	add(deleteSelectedButton);

	y += 2 * labelHeight;
	Button* addIndividualButton = new Button("Add individual");
	addIndividualButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	addIndividualButton->setSize(halfLineLabelSize);
	addIndividualButton->addManagedMouseListener(new AddIndividualButtonMouseListener(
			creationState.get()));
	add(addIndividualButton);

	y += labelHeight;
	Button* addBarrierButton = new Button("Add barrier");
	addBarrierButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	addBarrierButton->setSize(halfLineLabelSize);
	addBarrierButton->addManagedMouseListener(new AddBarrierButtonMouseListener(creationState.get()));
	add(addBarrierButton);

	y += labelHeight;
	Button* addMeatSupplyButton = new Button("Add meat");
	addMeatSupplyButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	addMeatSupplyButton->setSize(halfLineLabelSize);
	addMeatSupplyButton->addManagedMouseListener(new AddMeatButtonMouseListener(creationState.get()));
	add(addMeatSupplyButton);

	y += labelHeight;
	Button* addVegetableSupplyButton = new Button("Add vegetable");
	addVegetableSupplyButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	addVegetableSupplyButton->setSize(halfLineLabelSize);
	addVegetableSupplyButton->addManagedMouseListener(new AddVegetableButtonMouseListener(
			creationState.get()));
	add(addVegetableSupplyButton);

	y += labelHeight;
	Button* addWaterSupplyButton = new Button("Add water");
	addWaterSupplyButton->setPosition(Vector(halfLineLabelSize.getWidth() / 2, y));
	addWaterSupplyButton->setSize(halfLineLabelSize);
	addWaterSupplyButton->addManagedMouseListener(
			new AddWaterButtonMouseListener(creationState.get()));
	add(addWaterSupplyButton);

	y += 2 * labelHeight;
	addOnDragCheckbox = new Checkbox(false, "Add on mouse drag");
	addOnDragCheckbox->setPosition(Vector(0.0f, y));
	addOnDragCheckbox->setSize(fullLineLabelSize);
	add(addOnDragCheckbox);

	y += 2 * labelHeight;
	anchorToGridCheckbox = new Checkbox(false, "Anchor to grid");
	anchorToGridCheckbox->setPosition(Vector(0.0f, y));
	anchorToGridCheckbox->setSize(fullLineLabelSize);
	add(anchorToGridCheckbox);
}

int ControlPanel::getTimeSpeedMultiplication() const
{
	return speedSlider->getValue();
}

CreationState* ControlPanel::getCreationState()
{
	return creationState.get();
}

const Checkbox* ControlPanel::getAddOnDragCheckbox() const
{
	return addOnDragCheckbox;
}

const Checkbox* ControlPanel::getAnchorToGridCheckbox() const
{
	return anchorToGridCheckbox;
}
